package com.it.reggie.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.it.reggie.commen.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;


import java.time.LocalDateTime;


@Component
public class MyDateInsert implements MetaObjectHandler {

    /**
     * 新增时填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        // createTime 对应实体属性名, 第二个参数需要填充的值

        Long updateUser = BaseContext.getCurrentId();
        Long createUser = BaseContext.getCurrentId();

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", createUser);
        metaObject.setValue("updateUser", updateUser);
    }

    /**
     * 修改时填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Long updateUser = BaseContext.getCurrentId();
        // updateTime 对应实体属性名, 第二个参数需要填充的值
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", updateUser);
    }}